import 'package:flutter/material.dart';
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/color_common.dart';
import 'package:gzmall_app/common/net_util.dart';
import 'package:gzmall_app/common/toast_util.dart';
import 'package:gzmall_app/model/user.dart';

class ResetPhonePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => ResetPhonePageState();
}

class ResetPhonePageState extends State<ResetPhonePage> {
  String _newPhone, _verifyCode;
  User _user = User.empty();
  bool _isSendVerify = false;

  TextEditingController _phoneController = TextEditingController(),
      _codeController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _user.getUserInfo();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _createAppBar(),
      body: ListView(
        children: <Widget>[
          _createInputItem(false, "新的手机号码", (str) {
            _newPhone = str ?? '';
          }, _phoneController),
          _createInputItem(false, "验证码", (str) {
            _verifyCode = str ?? '';
          }, _codeController,
              child: new GestureDetector(
                child: new Text(
                  !_isSendVerify ? "发送验证码" : "验证码已发送",
                  textAlign: TextAlign.right,
                  style: new TextStyle(
                      color: !_isSendVerify
                          ? ColorCommon.primaryColor
                          : ColorCommon.grey1),
                ),
                onTap: () {
                  if (_user != null &&
                      _user.phone != null &&
                      _user.phone.isNotEmpty) {
                    _getVerifyCode(_newPhone, _user.phone);
                  }
                },
              )),
        ],
      ),
    );
  }

  _createAppBar() {
    return AppBar(
      elevation: 0.0,
      title: Text("修改手机号码"),
      leading: IconButton(
        icon: Icon(Icons.arrow_back_ios),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
      actions: <Widget>[
        GestureDetector(
          onTap: () {
            _resetPhoneAction();
          },
          child: Container(
            child: Text("完成"),
            alignment: Alignment.center,
            padding: const EdgeInsets.only(right: 15.0),
          ),
        ),
      ],
    );
  }

  _createInputItem(bool obscureText, String hint, Function onChanged,
      TextEditingController controller,
      {Widget child}) {
    return Container(
      height: 54.0,
      padding: const EdgeInsets.only(left: 20.0, right: 20.0),
      margin: const EdgeInsets.only(top: 20.0),
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white),
      child: Row(
        children: <Widget>[
          Expanded(
            child: TextField(
              obscureText: obscureText,
              controller: controller,
              style: TextStyle(color: ColorCommon.black1),
              decoration: InputDecoration.collapsed(
                hintText: hint,
                hintStyle: TextStyle(fontSize: 12.0, color: ColorCommon.grey1),
              ),
              onChanged: onChanged,
            ),
          ),
          child ?? Align(),
        ],
      ),
    );
  }

  _getVerifyCode(String newPhone, String oldPhone) {
    Map<String, String> params = {
      "phone": newPhone,
      "type": "phone",
      "old_phone": oldPhone,
    };

    NetUtil.post(context, Api.identifyCodes, (data, message) {
      _isSendVerify = true;
      ToastUtil.showMessageToast(message);
    }, params: params, isShowLoading: true);
  }

  _resetPhoneAction() {
    if (_newPhone == null || _newPhone.isEmpty) {
      ToastUtil.showMessageToast("请输入新的手机号码");
      return;
    }
    if (_verifyCode == null || _verifyCode.isEmpty) {
      ToastUtil.showMessageToast("请输入验证码");
      return;
    }

    _resetPhone(_newPhone, _verifyCode);
  }

  _resetPhone(String phone, String code) {
    Map<String, String> params = {
      "phone": phone,
      "code": code,
    };

    NetUtil.post(context, Api.userResetPhone, (data, message) {
      ToastUtil.showMessageToast(message);
      Navigator.of(context).pop();
    }, params: params, isShowLoading: true);
  }
}
